<template>
  <div class="accountinfo">
    <div class="head">
      <h2>管理员信息</h2>
    </div>
    <div class="main">
      <p class="accounttext">
        管理员ID: <span>{{ id }}</span>
      </p>
      <p class="accounttext">
        账户: <span>{{ account }}</span>
      </p>
      <p class="accounttext">
        创建时间: <span>{{ ctime }}</span>
      </p>
      <p class="accounttext">
        用户组: <span>{{ userGroup }}</span>
      </p>
      <div class="acatar-text">
        <span>管理员头像：</span>
        <el-upload
          class="avatar-uploader"
          action="http://127.0.0.1:5000/users/avatar_upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :data="params"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>
    </div>
  </div>
</template>

<script>
import { $_accountinfo } from "@/apis/user";
import { getChinatime } from "@/utils/utils";
import bus from "@/utils/bus";
export default {
  created() {
    $_accountinfo({
      id: localStorage.id,
    }).then((res) => {
      console.log(res.data);
      this.id = res.data.accountInfo.id;
      this.account = res.data.accountInfo.account;
      this.ctime = getChinatime(res.data.accountInfo.ctime);
      this.userGroup = res.data.accountInfo.userGroup;
    });
  },
  data() {
    return {
      id: "",
      ctime: "",
      account: "",
      userGroup: "",
      imgurl: "",
      imageUrl: "",
      params: { id: localStorage.id },
    };
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      bus.$emit("refreshHeader", this.imageUrl);
    },
    beforeAvatarUpload(file) {
      let types = ["image/jpeg", "image/png", "image/webp"];
      const isJPG = types.includes(file.type);
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG、png、webp 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="less" scoped>
.accountinfo {
  width: 98%;
  height: 100%;
  padding: 10px;
  background-color: #fff;
  box-shadow: 0 0 20px #212121;
  .head {
    height: 50px;
    padding: 0 10px;
    margin-bottom: 10px;
    border-bottom: 1px solid #bdbdbd;
  }
  .main {
    padding: 20px;
    .accounttext {
      height: 50px;
      line-height: 50px;
      padding: 0 10px;
      margin-bottom: 10px;
      border-bottom: 1px solid #bdbdbd;
    }
    .acatar-text {
      display: flex;
      align-items: center;
      .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
      }
      .avatar-uploader .el-upload:hover {
        border-color: #409eff;
      }
      .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
      }
      .avatar {
        width: 178px;
        height: 178px;
        display: block;
      }
    }
  }
}
</style>